﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace ChinaCustoms.Qingdao.Workflow.Controls
{
    /// <summary>
    /// ModalLink 的摘要说明
    /// </summary>
    /// <remarks>注意:本控件仅适用于IE浏览器,如果要在FireFox等浏览器下适用请使用拓宇公司控件库中相应控件</remarks>
    public class ModalLink : HyperLink
    {
        public ModalLink()
        {

        }

        protected override void OnPreRender(EventArgs e)
        {
            this.RegScript();
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.Attributes["href"] = string.Format("javascript:OpenModalDialog_{0}();", this.ID);
            base.Render(writer);
        }

        private void RegScript()
        {
            string scriptName = string.Format("Tunynet_ShowModalDialog_ScriptName_{0}", this.ID);

            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), scriptName, this.GetWindowScript(),false);

            //if (this.Page.ClientScript.IsClientScriptBlockRegistered(scriptName) == false)
            //{
            //    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), scriptName, this.GetWindowScript());
            //}
        }

        private string GetWindowScript()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<script type=\"text/javascript\" language=\"javascript\">");
            sb.AppendLine();
            sb.AppendLine("<!--");
            sb.AppendFormat("function OpenModalDialog_{0}()",this.ID);
            sb.AppendLine("{");
            sb.AppendLine("var vReturnValue;");
            sb.AppendFormat("vReturnValue = window.showModalDialog(\"{0}\",\"{1}\",\"{2}\");", this.DestinationURL, this.destinationArguments, this.GetWindowOpenPara());
            sb.AppendLine();
            if (string.IsNullOrEmpty(CallBack)==false)
            {
                sb.AppendFormat("{0}(vReturnValue);",this.CallBack);
            }

            sb.AppendLine("}");
            sb.AppendLine("//-->");
            sb.AppendLine("</script>");

            return sb.ToString();
        }

        private string GetWindowOpenPara()
        {
            string ret = string.Format("dialogWidth:{0}px;dialogHeight:{1}px;status:{2};resizable:{3};center:{4}",
                this.destinationWidth, this.destinationHeight, ConvertBoolToString(this.status),
                ConvertBoolToString(this.resizable), ConvertBoolToString(this.isCenter));
            return ret;
        }

        private string ConvertBoolToString(bool inputValue)
        {
            if (inputValue == true)
            {
                return "yes";
            }
            else
            {
                return "no";
            }
        }

        //private string destinationURL;
        /// <summary>
        /// 需要打开的子窗口的URL
        /// </summary>
        public string DestinationURL
        {
            get
            {
                if (ViewState["DestinationURL"]!=null)
                    return ViewState["DestinationURL"] as string;
                else
                    return string.Empty;

                //return this.Page.Response.ApplyAppPathModifier(destinationURL);
            }
            set 
            {
                ViewState["DestinationURL"] = ResolveUrl(value); 
            }
        }

        private string destinationArguments;
        /// <summary>
        /// 传送到子窗口的参数
        /// </summary>
        public string DestinationArguments
        {
            get { return destinationArguments; }
            set { destinationArguments = value; }
        }

        private int destinationWidth = 400;
        /// <summary>
        /// 需要打开的子窗口的宽度
        /// </summary>
        public int DestinationWidth
        {
            get { return destinationWidth; }
            set { destinationWidth = value; }
        }

        private int destinationHeight = 300;
        /// <summary>
        /// 需要打开的子窗口的高度
        /// </summary>
        public int DestinationHeight
        {
            get { return destinationHeight; }
            set { destinationHeight = value; }
        }

        private bool isCenter = true;
        /// <summary>
        /// 窗口是否居中，默认yes，但仍可以指定高度和宽度
        /// </summary>
        public bool IsCenter
        {
            get { return isCenter; }
            set { isCenter = value; }
        }

        private bool resizable = false;
        /// <summary>
        /// ［IE5+］：是否可被改变大小。默认no。
        /// </summary>
        public bool Resizable
        {
            get { return resizable; }
            set { resizable = value; }
        }

        private bool status = false;
        /// <summary>
        /// ［IE5+］：是否显示状态栏。默认为no。
        /// </summary>
        public bool Status
        {
            get { return status; }
            set { status = value; }
        }

        private bool scroll = true;
        /// <summary>
        /// 指明对话框是否显示滚动条。默认为yes。
        /// </summary>
        public bool Scroll
        {
            get { return scroll; }
            set { scroll = value; }
        }

        private string callBack;
        /// <summary>
        /// 子窗口关闭后的 回调函数(javascript函数)
        /// </summary>
        public string CallBack
        {
            get 
            {
                if (string.IsNullOrEmpty(callBack))
                {
                    return string.Empty;
                }
                else
                {
                    if (callBack.EndsWith("()"))
                    {
                        return callBack.Substring(0,callBack.IndexOf("()"));
                    }
                    else
                    {
                        return callBack;
                    }
                }
            }
            set { callBack = value; }
        }
	
    }
}
